package com.example.cia;

import java.util.List;

import com.example.cia.service.getDeviceService;
import com.example.cia.service.getSightsService;
import com.example.cia.service.modifyDeviceService;
import com.example.cia.support.IonClick;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class checkDevice extends Activity implements IonClick{
	
	private Button scanButton;
	private Button scanDevice;
	private EditText getDeviceState;
	
	/*
	 * 动态刷新获取页面
	 */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_check);
		
		scanButton = (Button) findViewById(R.id.scanButton);
		scanDevice = (Button) findViewById(R.id.scanDevice);
		
		scanButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent startScan = new Intent(checkDevice.this,com.xys.libzxing.zxing.activity.CaptureActivity.class);
				startActivityForResult(startScan,0);
			}
		});
		
		scanDevice.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent startScan = new Intent(checkDevice.this,com.xys.libzxing.zxing.activity.CaptureActivity.class);
				startActivityForResult(startScan,1);
			}
		});
	}
	
	public void onActivityResult(int requestCode,int resultCode,Intent data){
		switch(requestCode){
		case 0:
			if(resultCode == RESULT_OK){
				String text = data.getExtras().getString("result");
				int id = Integer.parseInt(text);
				String sightsDescription = new getSightsService().getSights(id);
				AlertDialog.Builder description = new AlertDialog.Builder(checkDevice.this);
				description.setTitle("扫描结果");
				description.setMessage(sightsDescription);
				final AlertDialog ad = description.show();
			
				description.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
				
					@Override
					public void onClick(DialogInterface dialog, int which) {
						ad.dismiss();
					}
				});
			}
			else{
				Toast.makeText(getApplicationContext(), "扫描失败！", Toast.LENGTH_LONG).show();
			};
		break;
		case 1:
			if(resultCode == RESULT_OK){
				String resultId = data.getExtras().getString("result");
				List<String> result = new getDeviceService().getDevice(resultId, 3);
				final String deviceId = result.get(0);
				String deviceDescription = result.get(1);
				String deviceState = result.get(2);
				
				AlertDialog.Builder deviceDialog = new AlertDialog.Builder(checkDevice.this);
				deviceDialog.setTitle("扫描结果");
				
				//使用LayoutInflater获取login_dialog.xml
		        LayoutInflater factory = LayoutInflater.from(checkDevice.this);  
		        //通过反射获取View
		        final View loginView = factory.inflate(R.layout.show_device_info, null);
		        		        
		        TextView getDeviceId = (TextView) loginView.findViewById(R.id.getDeviceId);
		        TextView getDeviceDes = (TextView) loginView.findViewById(R.id.getDeviceDescription);
		        getDeviceState = (EditText) loginView.findViewById(R.id.getDeviceState);
		        
		        deviceDialog.setView(loginView);
     
		        //初始化属性信息
		        getDeviceId.setText(deviceId);
		        getDeviceDes.setText(deviceDescription);
		        getDeviceState.setText(deviceState);
		       
		        
		        //对话框的各个按钮
		        deviceDialog.setNegativeButton("修改", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						modifyDeviceService mds = new modifyDeviceService();
						if(mds.modifyDevice(deviceId, Integer.parseInt(getDeviceState.getText().toString())))
							Toast.makeText(getApplicationContext(), "修改成功！", Toast.LENGTH_SHORT).show();
						else
							Toast.makeText(getApplicationContext(), "修改失败！", Toast.LENGTH_SHORT).show();
					}
				});
		        deviceDialog.setPositiveButton("关闭", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
					}
				});
		        deviceDialog.show();
			}
			else{
				Toast.makeText(getApplicationContext(), "扫描失败！", Toast.LENGTH_LONG).show();
			}
		}
	}
	
	public void checkDeviceOnClick(View v){
		
	}
	
	public void addDeviceOnClick(View v){
		Intent intent = new Intent(this,addDevice.class);
		startActivity(intent);
	}
	
	public void aboutAppOnClick(View v){
		Intent intent = new Intent(this,aboutApp.class);
		startActivity(intent);
	}

	public void mainPageOnClick(View v) {
		Intent intent = new Intent(this,MainActivity.class);
		startActivity(intent);
	}  
	
	
}
